use crate::dto::*;
use crate::entity::*;
use crate::RB;
use hypers::prelude::{hypers_openapi, openapi, OpenApi, Responder, Response};
use rbatis::plugin::page::PageRequest;
use rbatis::rbdc::datetime::DateTime;

pub struct Title;

#[openapi(
    prefix = "/api",
    components(schemas(TitleSaveReq, TitleDeleteReq, TitleUpdateReq, TitleListReq))
)]
impl Title {
    #[post("title_save",tag = "添加面试题目",request_body = TitleSaveReq)]
    async fn title_save(item: TitleSaveReq) -> impl Responder {
        log::info!("title_save params: {:?}", &item);
        let title = InterviewTitle {
            id: None,
            title: item.title,
            content: item.content,
            interview_type: item.interview_type,
            create_time: Some(DateTime::now()),
            update_time: Some(DateTime::now()),
        };
        let result = InterviewTitle::insert(&mut RB.clone(), &title).await;
        handle_result(result)
    }

    #[post("title_delete",tag = "删除面试题目",request_body = TitleDeleteReq)]
    async fn title_delete(item: TitleDeleteReq) -> impl Responder {
        log::info!("title_delete params: {:?}", &item);
        let result = InterviewTitle::delete_in_column(&mut RB.clone(), "id", &item.ids).await;
        handle_result(result)
    }

    #[post("title_update",tag = "更新面试题目",request_body = TitleUpdateReq)]
    async fn title_update(item: TitleUpdateReq) -> impl Responder {
        log::info!("title_update params: {:?}", &item);
        let title = InterviewTitle {
            id: item.id,
            title: item.title,
            content: item.content,
            interview_type: item.interview_type,
            create_time: None,
            update_time: Some(DateTime::now()),
        };
        let result = InterviewTitle::update_by_column(&mut RB.clone(), &title, "id").await;
        handle_result(result)
    }

    #[post("title_list",tag = "查询面试题目",request_body = TitleListReq)]
    async fn title_list(item: TitleListReq) -> impl Responder {
        log::info!("title_list params: {:?}", &item);
        let page = &PageRequest::new(item.page_no, item.page_size);
        let result = InterviewTitle::select_page(&mut RB.clone(), page).await;
        let mut res = Response::default();
        match result {
            Ok(d) => {
                let mut title_list_data: Vec<TitleListData> = Vec::new();
                for x in d.records {
                    title_list_data.push(TitleListData {
                        id: x.id,
                        title: x.title,
                        content: x.content,
                        interview_type: x.interview_type,
                        create_time: x.create_time.unwrap().0.to_string(),
                        update_time: x.update_time.unwrap().0.to_string(),
                    })
                }
                res.json(&ok_result_page(title_list_data, d.total));
                res
            }
            Err(err) => {
                log::error!("{}", err.to_string());
                res.json(&err_result_page(err.to_string()));
                res
            }
        }
    }
}
